package znbkTopicType;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;

import com.example.lancooznbk.R;
import com.example.lancooznbk.view.base.BaseActivity;
import com.example.lancooznbk.view.base.BaseFragment;
import com.example.lancooznbk.view.base.ILoadPaperView;
import com.example.lancooznbk.view.widget.LoadingDialog;
import com.example.lancooznbk.view.widget.ScaleButton;
import com.lancoo.rxretrofitlibrary.Api.UserInfo;

import lancooControl.AndroidBug5497Workaround;
import lancooControl.CustomEditText;
import znbkChangeNote.SaveNoteEntityModel;
import znbkChangeNote.entity.DeleteNoteEntity;
import znbkChangeNote.entity.SaveNoteEntity;
import znbkChangeNote.presenter.DeleteNotePresenter;
import znbkChangeNote.presenter.SaveNotePresenter;
import znbkTestPaperCtfd.fragment.WrongTopicTutoringPaperFragment;
import znbkTestPaperInfor.ZnbkPaperDef;
import znbkTestPaperInfor.ZnbkPaperInfor;
import znbkTestPaperLstjt.fragment.HistoryRecommendQuesPaperFragment;
import znbkTestPaperNtxl.activity.TrainingDifficultProblemsPaperActivity;
import znbkTestPaperNtxl.fragment.TrainingDifficultProblemsPaperFragment;
import znbkTestPaperRdqh.fragment.WeekAscensionPaperFragment;
import znbkTestPaperRdtz.activity.HeatPointExtensionPaperSplActivity;
import znbkTestPaperRdtz.fragment.HeatPointExtensionPaperFragment;
import znbkTestPaperXtfx.activity.SystemReviewPaperActivity;
import znbkTestPaperXtfx.fragment.SystemReviewPaperFragment;

import static znbkTestPaperCtfd.activity.WrongTopicTutoringPaperActivity.mEntityCtfd;
import static znbkTestPaperNtxl.activity.TrainingDifficultProblemsPaperActivity.mEntityNtxl;

/**
 * Created by Administrator on 2018\4\16 0016.
 */

public class NoteDetailActivity extends BaseActivity implements ILoadPaperView{

    private static String NoteContent;
    private static String Title;
    private static int QuesOrPaperFrom;
    private static String NoteID;
    private static String ResType;
    private static String QuesID;

    CustomEditText CEdtNotes;
    private ScaleButton mSBtnBack;//返回按钮
    private TextView mTvTittle;
    private ScaleButton mSBtnMore;
    private ScaleButton mSBtnOriginalQuestion;

    public  static void launch(BaseActivity activity, String noteContent,String title,String noteID,String resType,
                               String quesID, @ZnbkPaperDef.PaperFrom int quesOrPaperFrom){
        NoteContent = noteContent;
        Title = title;
        NoteID = noteID;
        ResType = resType;
        QuesID = quesID;
        QuesOrPaperFrom = quesOrPaperFrom;
        Intent intent = new Intent(activity, NoteDetailActivity.class);
        activity.startActivityForResult(intent,1);
    }

    public  static void launch(BaseFragment fragment, String noteContent,String title,String noteID,String resType,
                               String quesID, @ZnbkPaperDef.PaperFrom int quesOrPaperFrom){
        NoteContent = noteContent;
        Title = title;
        NoteID = noteID;
        ResType = resType;
        QuesID = quesID;
        QuesOrPaperFrom = quesOrPaperFrom;
        Intent intent = new Intent(fragment.getContext(), NoteDetailActivity.class);
        fragment.startActivityForResult(intent,1);
    }


    @Override
    public int attachLayoutRes() {
        return R.layout.znbk_layout_add_notes_view;
    }

    @Override
    protected void initViews() {
        AndroidBug5497Workaround.assistActivity(this);

        //核心代码.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }

        /*
        * 返回按钮菜单相关设置
        * */
        mSBtnBack = (ScaleButton) findViewById(R.id.sbtn_back);
        mSBtnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!NoteContent.equals("")){
                    SaveNoteEntity saveNoteEntity = SaveNoteEntityModel.getSaveNotesEntity(
                            SaveNoteEntityModel.getNoteBean(UserInfo.StuId, ResType,
                                    UserInfo.SubjectId,NoteID,UserInfo.StageNo,QuesID),Title,NoteContent);
                    saveNote(saveNoteEntity);
                    if(QuesOrPaperFrom==ZnbkPaperDef.FROM_WEEK_ASCENSION){
                        WeekAscensionPaperFragment.IsHaveNoteRdqh = true;
                    }else if(QuesOrPaperFrom==ZnbkPaperDef.FROM_SYSTEM_REVIEW){
                        SystemReviewPaperFragment.IsHaveNoteXtfx = true;
                    }else if(QuesOrPaperFrom==ZnbkPaperDef.FROM_TRAINING_DIFFICULT_PROBLEMS){
                        TrainingDifficultProblemsPaperFragment.IsHaveNoteNtxl = true;
                    }else if(QuesOrPaperFrom==ZnbkPaperDef.FROM_WRONG_TOPIC_TUTORING){
                        WrongTopicTutoringPaperFragment.IsHaveNoteCtfd = true;
                    }else if(QuesOrPaperFrom==ZnbkPaperDef.FROM_HISTORY_RECOMMEND_QUESTION){
                        HistoryRecommendQuesPaperFragment.IsHaveNoteLstjt = true;
                    }else if(QuesOrPaperFrom==ZnbkPaperDef.FROM_HEAT_POINT_EXTENSION){
                        HeatPointExtensionPaperFragment.IsHaveNoteRdtz = true;
                    }else if(QuesOrPaperFrom==ZnbkPaperDef.FROM_HEAT_POINT_EXTENSION_SPL){
                        HeatPointExtensionPaperSplActivity.IsHaveNoteRdtzSpl = true;
                    }

                }else{
                    DeleteNoteEntity deleteNoteEntity = new DeleteNoteEntity(UserInfo.StuId,UserInfo.SubjectId,
                            UserInfo.StageNo,NoteID,ResType,QuesID);
                    deleteNote(deleteNoteEntity);
                    if(QuesOrPaperFrom==ZnbkPaperDef.FROM_WEEK_ASCENSION){
                        WeekAscensionPaperFragment.IsHaveNoteRdqh = false;
                    }else if(QuesOrPaperFrom==ZnbkPaperDef.FROM_SYSTEM_REVIEW){
                        SystemReviewPaperFragment.IsHaveNoteXtfx = false;
                    }else if(QuesOrPaperFrom==ZnbkPaperDef.FROM_TRAINING_DIFFICULT_PROBLEMS){
                        TrainingDifficultProblemsPaperFragment.IsHaveNoteNtxl = false;
                    }else if(QuesOrPaperFrom==ZnbkPaperDef.FROM_WRONG_TOPIC_TUTORING){
                        WrongTopicTutoringPaperFragment.IsHaveNoteCtfd = false;
                    }else if(QuesOrPaperFrom==ZnbkPaperDef.FROM_HISTORY_RECOMMEND_QUESTION){
                        HistoryRecommendQuesPaperFragment.IsHaveNoteLstjt = false;
                    }else if(QuesOrPaperFrom==ZnbkPaperDef.FROM_HEAT_POINT_EXTENSION){
                        HeatPointExtensionPaperFragment.IsHaveNoteRdtz = false;
                    }else if(QuesOrPaperFrom==ZnbkPaperDef.FROM_HEAT_POINT_EXTENSION_SPL){
                        HeatPointExtensionPaperSplActivity.IsHaveNoteRdtzSpl = false;
                    }

                }
                HideKeyboard();
                finish();
            }
        });


        /*
        * 页面头部-》笔记详情：mTvTittle
        * */
        mTvTittle = (TextView) findViewById(R.id.tv_title);
        mTvTittle.setText(R.string.znbk_titleNoteDetails);

        /*
        * 更多功能按钮菜单相关信息
        * */
        mSBtnMore = (ScaleButton) findViewById(R.id.sbtn_more);
        mSBtnMore.setVisibility(View.GONE);


        CEdtNotes = (CustomEditText) findViewById(R.id.cedt_notes);
        CEdtNotes.setText(NoteContent);
        CEdtNotes.setSelection(NoteContent.length());
        CEdtNotes.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                NoteContent = s.toString();
            }
        });

        mSBtnOriginalQuestion = (ScaleButton) findViewById(R.id.sbtn_CheckOriginalQuestion);
        mSBtnOriginalQuestion.setVisibility(View.GONE);


    }

    @Override
    protected void updateViews(boolean isRefresh) {

    }

    /*
    * 保存笔记
    * */
    SaveNotePresenter mSaveNotePresenter;
    private void saveNote(SaveNoteEntity saveNoteEntity){
        mSaveNotePresenter = new SaveNotePresenter(this,QuesOrPaperFrom,saveNoteEntity);
        mSaveNotePresenter.getData(false);
    }

    /*
    * 删除笔记
    * */
    DeleteNotePresenter mDeleteNotePresenter;
    private void deleteNote(DeleteNoteEntity deleteNoteEntity){
        mDeleteNotePresenter = new DeleteNotePresenter(this,QuesOrPaperFrom,deleteNoteEntity);
        mDeleteNotePresenter.getData(false);
    }


    //隐藏虚拟键盘:Fragment进行切换的时候使用
    public  void HideKeyboard() {
        /**隐藏软键盘**/
        View viewK = this.getWindow().peekDecorView();
        if(viewK != null){
            InputMethodManager inputmanger = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
            inputmanger.hideSoftInputFromWindow(viewK.getWindowToken(), 0);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    /*
    * 监听系统home事件
    * */
    @Override
    protected void onUserLeaveHint() {
        super.onUserLeaveHint();
    }

    /*
    * 监听系统返回事件
    * */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            if(!NoteContent.equals("")){
                SaveNoteEntity saveNoteEntity = SaveNoteEntityModel.getSaveNotesEntity(
                        SaveNoteEntityModel.getNoteBean(UserInfo.StuId, ResType,
                                UserInfo.SubjectId,NoteID,UserInfo.StageNo,QuesID),Title,NoteContent);
                saveNote(saveNoteEntity);
                if(QuesOrPaperFrom==ZnbkPaperDef.FROM_WEEK_ASCENSION){
                    WeekAscensionPaperFragment.IsHaveNoteRdqh = true;
                }else if(QuesOrPaperFrom==ZnbkPaperDef.FROM_SYSTEM_REVIEW){
                    SystemReviewPaperFragment.IsHaveNoteXtfx = true;
                }else if(QuesOrPaperFrom==ZnbkPaperDef.FROM_TRAINING_DIFFICULT_PROBLEMS){
                    TrainingDifficultProblemsPaperFragment.IsHaveNoteNtxl = true;
                }else if(QuesOrPaperFrom==ZnbkPaperDef.FROM_WRONG_TOPIC_TUTORING){
                    WrongTopicTutoringPaperFragment.IsHaveNoteCtfd = true;
                }else if(QuesOrPaperFrom==ZnbkPaperDef.FROM_HISTORY_RECOMMEND_QUESTION){
                    HistoryRecommendQuesPaperFragment.IsHaveNoteLstjt = true;
                }else if(QuesOrPaperFrom==ZnbkPaperDef.FROM_HEAT_POINT_EXTENSION){
                    HeatPointExtensionPaperFragment.IsHaveNoteRdtz = true;
                }else if(QuesOrPaperFrom==ZnbkPaperDef.FROM_HEAT_POINT_EXTENSION_SPL){
                    HeatPointExtensionPaperSplActivity.IsHaveNoteRdtzSpl = true;
                }

            }else{
                DeleteNoteEntity deleteNoteEntity = new DeleteNoteEntity(UserInfo.StuId,UserInfo.SubjectId,
                        UserInfo.StageNo,NoteID,ResType,QuesID);
                deleteNote(deleteNoteEntity);
                if(QuesOrPaperFrom==ZnbkPaperDef.FROM_WEEK_ASCENSION){
                    WeekAscensionPaperFragment.IsHaveNoteRdqh = false;
                }else if(QuesOrPaperFrom==ZnbkPaperDef.FROM_SYSTEM_REVIEW){
                    SystemReviewPaperFragment.IsHaveNoteXtfx = false;
                }else if(QuesOrPaperFrom==ZnbkPaperDef.FROM_TRAINING_DIFFICULT_PROBLEMS){
                    TrainingDifficultProblemsPaperFragment.IsHaveNoteNtxl = false;
                }else if(QuesOrPaperFrom==ZnbkPaperDef.FROM_WRONG_TOPIC_TUTORING){
                    WrongTopicTutoringPaperFragment.IsHaveNoteCtfd = false;
                }else if(QuesOrPaperFrom==ZnbkPaperDef.FROM_HISTORY_RECOMMEND_QUESTION){
                    HistoryRecommendQuesPaperFragment.IsHaveNoteLstjt = false;
                }else if(QuesOrPaperFrom==ZnbkPaperDef.FROM_HEAT_POINT_EXTENSION){
                    HeatPointExtensionPaperFragment.IsHaveNoteRdtz = false;
                }else if(QuesOrPaperFrom==ZnbkPaperDef.FROM_HEAT_POINT_EXTENSION_SPL){
                    HeatPointExtensionPaperSplActivity.IsHaveNoteRdtzSpl = false;
                }

            }
            finish();
            return false;
        }
        return false;
    }

    @Override
    public void loadPaperData(Object data) {

    }

    @Override
    public void loadComplete(boolean hasComplete) {

    }

    @Override
    public void openExamReportDialog() {

    }

    @Override
    public void openAssessDialog() {

    }

    @Override
    public void updatingResult(boolean isSucceed) {

    }

    @Override
    public void updatingReportResult(boolean isReportSucceed) {

    }

    @Override
    public LoadingDialog getLoadingDialog() {
        return null;
    }
}